﻿using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class CPlugin : MonoBehaviour {
 
	int i = 0 ;
	float f = 0.0f;
 
	//引用C 、C++中的方法
    [DllImport ("UnityCPlugin")]
    private static extern int addInt(int a,int b);
    [DllImport ("UnityCPlugin")]
	private static extern float addFloat(float a,float b);	
 
	void Start ()
	{
		//调用方法中相加函数
		i = addInt (1,2);
		f = addFloat (1.0f,2.2f);
		//将相加后的信息显示在屏幕中
		toastText(" use c = " +  i + " cplus = " +  f );
	}
	
	private void toastText(object str) {
		AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
		AndroidJavaObject currentActivity = UnityPlayer.GetStatic < AndroidJavaObject > ("currentActivity");
		AndroidJavaClass Toast = new AndroidJavaClass("android.widget.Toast");
		AndroidJavaObject context = currentActivity.Call < AndroidJavaObject > ("getApplicationContext");
		currentActivity.Call("runOnUiThread", new AndroidJavaRunnable(() => {
					AndroidJavaObject javaString = new AndroidJavaObject("java.lang.String", str.ToString());
					Toast.CallStatic < AndroidJavaObject > ("makeText", context, javaString, Toast.GetStatic < int > ("LENGTH_SHORT")).Call("show");
				}));
	}

}